package com.lanhai.utils;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;

import com.lanhai.Main;
import com.lanhai.MainAction;
import com.lanhai.system.Accept4Park;
/**
 * UDP协议发送数据工具类
 * @author liangmin
 *
 */
public class UDPUtil {
    
	/**
	 * 代理分发
	 * @param data
	 * @param ipAddress
	 * @param port
	 */
	 public static void sendData4Agent(byte[] data, String ipAddress, int port){
		   ByteArrayOutputStream baos = new ByteArrayOutputStream();
		   DataOutputStream dos = new DataOutputStream(baos);
		   DatagramSocket ds = null;
		   try{
			   dos.write(data);
			   byte[] buf = data;
			   DatagramPacket dp = new DatagramPacket(buf, buf.length, new InetSocketAddress(ipAddress, port));
			   System.out.println("代理主机发送:" + "ip:" + ipAddress + ":" + port + ",data:" + HexUtil.byte2HexStr(data));
			   InetAddress is = InetAddress.getByName("10.247.43.12");
			   ds = new DatagramSocket(0,is);//让java随机指定端口
//			   ds = new DatagramSocket(0);//让java随机指定端口
			   ds.send(dp);
		   }catch(Exception e){
			   e.printStackTrace();
		   }finally{
			   ds.close();
		   }
	   }	
	
	   public static void sendData(byte[] data, String ipAddress, int port){
		   ByteArrayOutputStream baos = new ByteArrayOutputStream();
		   DataOutputStream dos = new DataOutputStream(baos);
		   DatagramSocket ds = null;
		   try{
			   dos.write(data);
			   byte[] buf = baos.toByteArray();
			   DatagramPacket dp = new DatagramPacket(buf, buf.length, new InetSocketAddress(ipAddress, port));
			   System.out.println("ip:" + ipAddress + ":" + port + ",data:" + HexUtil.byte2HexStr(data));
			   InetAddress is = InetAddress.getByName("10.23.102.247");
//			   ds = new DatagramSocket(0,is);//让java随机指定端口
			   ds = new DatagramSocket(0);//让java随机指定端口
			   ds.send(dp);
		   }catch(Exception e){
			   e.printStackTrace();
		   }finally{
			   ds.close();
		   }
	   }
	   
	   public static void sendData4Agv(byte[] data, String ipAddress, int port){
		   ByteArrayOutputStream baos = new ByteArrayOutputStream();
		   DataOutputStream dos = new DataOutputStream(baos);
		   DatagramSocket ds = null;
		   try{
			   dos.write(data);
			   byte[] buf = baos.toByteArray();
			   DatagramPacket dp = new DatagramPacket(buf, buf.length, new InetSocketAddress(ipAddress, port));
			   System.out.println("ip:" + ipAddress + ":" + port + ",data:" + HexUtil.byte2HexStr(data));
			   InetAddress is = InetAddress.getByName("10.23.101.248");
//			   ds = new DatagramSocket(0,is);//让java随机指定端口
			   ds = new DatagramSocket(0);//让java随机指定端口
			   ds.send(dp);
		   }catch(Exception e){
			   e.printStackTrace();
		   }finally{
			   ds.close();
		   }
	   }	
	   
	   public static void sendData4Park(byte[] data, String ipAddress, int port){
		   ByteArrayOutputStream baos = new ByteArrayOutputStream();
		   DataOutputStream dos = new DataOutputStream(baos);
		   DatagramSocket ds = null;
		   try{
			   dos.write(data);
			   byte[] buf = baos.toByteArray();
			   DatagramPacket dp = new DatagramPacket(buf, buf.length, new InetSocketAddress(ipAddress, port));
			   System.out.println("ip:" + ipAddress + ":" + port + ",data:" + HexUtil.byte2HexStr(data));
			   InetAddress is = InetAddress.getByName(Main.localStationIpAddress);
			   ds = new DatagramSocket(0,is);//让java随机指定端口
//			   ds = new DatagramSocket();
			   ds.send(dp);
		   }catch(Exception e){
			   e.printStackTrace();
		   }finally{
			   ds.close();
		   }
	   }
	   
	   public static void sendData4Park4ZDES(byte[] data, String ipAddress, int port){
		   ByteArrayOutputStream baos = new ByteArrayOutputStream();
		   DataOutputStream dos = new DataOutputStream(baos);
		   DatagramSocket ds = null;
		   try{
			   dos.write(data);
			   byte[] buf = baos.toByteArray();
			   DatagramPacket dp = new DatagramPacket(buf, buf.length, new InetSocketAddress(ipAddress, port));
			   System.out.println("ip:" + ipAddress + ":" + port + ",data:" + HexUtil.byte2HexStr(data));
			   InetAddress is = InetAddress.getByName(Main.localStationIpAddress);
			   ds = new DatagramSocket(8190,is);//让java随机指定端口
//			   ds = new DatagramSocket();
			   ds.send(dp);
		   }catch(Exception e){
			   e.printStackTrace();
		   }finally{
			   ds.close();
		   }
	   }
	   
	   public static void sendData4ParkWithLocalPort(byte[] data, String ipAddress, int port){
		   ByteArrayOutputStream baos = new ByteArrayOutputStream();
		   DataOutputStream dos = new DataOutputStream(baos);
		   DatagramSocket ds = null;
		   try{
			   dos.write(data);
			   byte[] buf = baos.toByteArray();
			   DatagramPacket dp = new DatagramPacket(buf, buf.length, new InetSocketAddress(ipAddress, port));
			   System.out.println("ip:" + ipAddress + ":" + port + ",data:" + HexUtil.byte2HexStr(data));
			   Accept4Park.sendData(dp);
		   }catch(Exception e){
			   e.printStackTrace();
		   }
	   }
	   
	   public static void sendData4Byte(byte[] data, String ipAddress, int port){
		   ByteArrayOutputStream baos = new ByteArrayOutputStream();
		   DataOutputStream dos = new DataOutputStream(baos);
		   DatagramSocket ds = null;
		   try{
			   dos.write(data);
			   byte[] buf = baos.toByteArray();
			   DatagramPacket dp = new DatagramPacket(buf, buf.length, new InetSocketAddress(ipAddress, port));
			   //System.out.println("ip:" + ipAddress + ",data:" + HexUtil.byte2HexStr(data));
			   ds = new DatagramSocket(9998);
			   ds.send(dp);		   
		   }catch(Exception e){
			   e.printStackTrace();
		   }finally{
			   ds.close();
		   }
	   }	   


	   /**
	    * 发送Fins_udp数据
	    * @param data
	    * @param ipAddress
	    * @param port
	    * @param sendPort
	    */
	   public static byte[] sendData4Fins(byte[] data, String ipAddress, int port, int option, MainAction mainAction, int isRead){
		   DatagramSocket ds = null;
		   int blen = 22;
		   if(option == 2){
			   blen = 14;
		   }
		   byte[] recBuf = new byte[blen];
		   try{
			   InetAddress ia = InetAddress.getLocalHost();
			   ds = new DatagramSocket(0, ia);//使用随机端口发送数据
			   ds.setSoTimeout(1000);//设置超时为1秒钟
			   DatagramPacket dp = new DatagramPacket(data, data.length, new InetSocketAddress(ipAddress, port));
			   System.out.println("ip:" + ipAddress + ":" + port + ",data:" + HexUtil.byte2HexStr(data));
			   ds.send(dp);
			   DatagramPacket rdp = new DatagramPacket(recBuf, recBuf.length);
			   ds.receive(rdp);
			   ds.getPort();
			   mainAction.finsResponse(recBuf, rdp.getAddress().getHostAddress(), isRead,port);
		   }catch(Exception e){
			   e.printStackTrace();
		   }
		   finally{
			   ds.close();
		   }
		   return recBuf;
	   }	

	   public static byte[] sendData4Fins(byte[] data, String ipAddress, int port, int option, int isRead){
		   DatagramSocket ds = null;
		   int blen = 22;
		   if(option == 2){
			   blen = 14;
		   }
		   byte[] recBuf = new byte[blen];
		   try{
			   //InetAddress ia = InetAddress.getLocalHost();
			   InetAddress ia = InetAddress.getByName("192.168.250.117");
			   ds = new DatagramSocket(0, ia);//使用随机端口发送数据
			   ds.setSoTimeout(1000);//设置超时为1秒钟
			   DatagramPacket dp = new DatagramPacket(data, data.length, new InetSocketAddress(ipAddress, port));
			   System.out.println("ip:" + ipAddress + ":" + port + ",data:" + HexUtil.byte2HexStr(data));
			   ds.send(dp);
			   DatagramPacket rdp = new DatagramPacket(recBuf, recBuf.length);
			   ds.receive(rdp);
//			   mainAction.finsResponse(recBuf, rdp.getAddress().getHostAddress(), isRead);
		   }catch(Exception e){
			   e.printStackTrace();
		   }
		   finally{
			   ds.close();
		   }
		   return recBuf;
	   }	   
	   
}
